package com.log4jviewer.ui.preferences.filters;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;

import com.log4jviewer.filters.FilterModel;

/**
 * Create possibility modify the Description cell in Filters Table
 * 
 * @author Daniil Lopatin
 */
public class FilterDescriptionCellEditor extends EditingSupport {

    private final TableViewer tableViewer;

    public FilterDescriptionCellEditor(final TableViewer tableViewer) {
        super(tableViewer);
        this.tableViewer = tableViewer;
    }

    @Override
    protected CellEditor getCellEditor(final Object element) {
        return new TextCellEditor(tableViewer.getTable());
    }

    @Override
    protected boolean canEdit(final Object element) {
        return true;
    }

    @Override
    protected Object getValue(final Object element) {
        return ((FilterModel) element).getFilterDescr();
    }

    @Override
    protected void setValue(final Object element, final Object value) {
        String filterDescription = String.valueOf(value);
        ((FilterModel) element).setFilterDescr(filterDescription);
        tableViewer.update(element, null);
    }
}
